﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// GridViewTemplate 的摘要说明
/// </summary>
public class GridViewLableTemplate : ITemplate
{
    // 数据列名
    private string fieldName;
    // 控件ID
    private string controlId;

    private bool isCount;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="controlId"></param>
    /// <param name="fieldName"></param>
    public GridViewLableTemplate(string controlId, string fieldName,bool isCount)
    {
        this.controlId = controlId;
        this.fieldName = fieldName;
        this.isCount = isCount;
    }

    /**/
    /// <summary>
    /// 实现ITemplate的InstantiateIn方法
    /// </summary>
    /// <param name="container">控件容器</param>
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Label lbl = new Label();
        lbl.ID = controlId;
        lbl.DataBinding += new EventHandler(LabelTemplate_DataBinding);
        container.Controls.Add(lbl);
    }
    /**/
    /// <summary>
    /// 绑定事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LabelTemplate_DataBinding(object sender, System.EventArgs e)
    {
        Label lbl;
        lbl = (Label)sender;
        GridViewRow container = (GridViewRow)lbl.NamingContainer;
        if (this.isCount)
        {
            lbl.Text +=  DataBinder.Eval(container.DataItem, fieldName);
        }
        else
        {
            lbl.Text += "%" + DataBinder.Eval(container.DataItem, fieldName);
        }
        
    }
}
